<form action="<?php echo $action; ?>" method="post" enctype="multipart/form-data" id="speedy_form">
  <div class="content">
    <table>
      <tr>
        <td style="text-align: right;" width="200"><?php echo $entry_shipping_to; ?></td>
        <td>&nbsp; &nbsp; &nbsp;<input type="radio" id="speedy_shipping_to_door" name="to_office" value="0" <?php if (!$to_office) { ?> checked="checked"<?php } ?> onclick="$('#speedy_quarter_container,#speedy_street_container,#speedy_block_no_container,#speedy_note_container').show(); $('#speedy_office_container').hide();" />
          <label for="speedy_shipping_to_door"><?php echo $text_to_door; ?></label>
          <input type="radio" id="speedy_shipping_to_office" name="to_office" value="1" <?php if ($to_office) { ?> checked="checked"<?php } ?> onclick="$('#speedy_quarter_container,#speedy_street_container,#speedy_block_no_container,#speedy_note_container').hide(); $('#speedy_office_container').show();" />
          <label for="speedy_shipping_to_office"><?php echo $text_to_office; ?></label></td>
      </tr>
      <tr>
        <td style="text-align: right;"><label for="speedy_city"><?php echo $entry_city; ?></label></td>
        <td><input type="text" id="speedy_city" name="city" value="<?php echo $city; ?>" size="50" />
          <input type="hidden" id="speedy_city_id" name="city_id" value="<?php echo $city_id; ?>" />
          <input type="hidden" id="speedy_city_nomenclature" name="city_nomenclature" value="<?php echo $city_nomenclature; ?>" />
          <label for="speedy_postcode"><?php echo $entry_postcode; ?></label>
          <input type="text" id="speedy_postcode" name="postcode" value="<?php echo $postcode; ?>" disabled="disabled" size="3" /></td>
      </tr>
      <tr id="speedy_quarter_container" <?php if ($to_office) { ?> style="display: none;"<?php } ?>>
        <td style="text-align: right;"><label for="speedy_quarter"><?php echo $entry_quarter; ?></label></td>
        <td><input type="text" id="speedy_quarter" name="quarter" value="<?php echo $quarter; ?>" size="50" />
          <input type="hidden" id="speedy_quarter_id" name="quarter_id" value="<?php echo $quarter_id; ?>" /></td>
      </tr>
      <tr id="speedy_street_container" <?php if ($to_office) { ?> style="display: none;"<?php } ?>>
        <td style="text-align: right;"><label for="speedy_street"><?php echo $entry_street; ?></label></td>
        <td><input type="text" id="speedy_street" name="street" value="<?php echo $street; ?>" size="50" />
          <input type="hidden" id="speedy_street_id" name="street_id" value="<?php echo $street_id; ?>" />
          <label for="speedy_street_no"><?php echo $entry_street_no; ?></label>
          <input type="text" id="speedy_street_no" name="street_no" value="<?php echo $street_no; ?>" size="3" /></td>
      </tr>
      <tr id="speedy_block_no_container" <?php if ($to_office) { ?> style="display: none;"<?php } ?>>
        <td style="text-align: right;"><label for="speedy_block_no"><?php echo $entry_block_no; ?></label></td>
        <td><input type="text" id="speedy_block_no" name="block_no" value="<?php echo $block_no; ?>" size="18" />
          <label for="speedy_entrance_no"><?php echo $entry_entrance_no; ?></label>
          <input type="text" id="speedy_entrance_no" name="entrance_no" value="<?php echo $entrance_no; ?>" size="3" />
          <label for="speedy_floor_no"><?php echo $entry_floor_no; ?></label>
          <input type="text" id="speedy_floor_no" name="floor_no" value="<?php echo $floor_no; ?>" size="3" />
          <label for="speedy_apartment_no"><?php echo $entry_apartment_no; ?></label>
          <input type="text" id="speedy_apartment_no" name="apartment_no" value="<?php echo $apartment_no; ?>" size="3" />
          <?php if ($error_address) { ?>
          <span class="error"><?php echo $error_address; ?></span>
          <?php } ?></td>
      </tr>
      <tr id="speedy_note_container" <?php if ($to_office) { ?> style="display: none;"<?php } ?>>
        <td style="text-align: right;"><label for="speedy_note"><?php echo $entry_note; ?></label></td>
        <td><input type="text" id="speedy_note" name="note" value="<?php echo $note; ?>" size="50" /></td>
      </tr>
      <tr id="speedy_office_container" <?php if (!$to_office) { ?> style="display: none;"<?php } ?>>
        <td style="text-align: right;"><label for="speedy_office_id"><?php echo $entry_office; ?></label></td>
        <td><select id="speedy_office_id" name="office_id" style="width: 400px;">
            <?php if (!$offices) { ?><option value="0" selected="selected"><?php echo $text_select_city; ?></option><?php } ?>
            <?php foreach ($offices as $office) { ?>
            <?php if ($office['id'] == $office_id) { ?>
            <option value="<?php echo $office['id']; ?>" selected="selected"><?php echo $office['label']; ?></option>
            <?php } else { ?>
            <option value="<?php echo $office['id']; ?>"><?php echo $office['label']; ?></option>
            <?php } ?>
            <?php } ?>
          </select>
          <?php if ($error_office) { ?>
          <span class="error"><?php echo $error_office; ?></span>
          <?php } ?></td>
      </tr>
      <tr <?php if (!$fixed_time) { ?> style="display: none;"<?php } ?>>
        <td style="text-align: right;"><input style="float: right;" type="checkbox" id="speedy_fixed_time_cb" name="fixed_time_cb" value="1" <?php if ($fixed_time_cb) { ?> checked="checked"<?php } ?> <?php if (!$fixed_time_cb_enable) { ?> disabled="disabled"<?php } ?> onclick="speedyCheckFixedTime();" />
          <label for="speedy_fixed_time_cb"><?php echo $entry_fixed_time; ?></label></td>
        <td><select id="speedy_fixed_time_hour" name="fixed_time_hour" <?php if (!$fixed_time_cb) { ?> disabled="disabled"<?php } ?> onchange="speedySetFixedTime();">
            <?php for ($i = 10; $i <= 17; $i++) { ?>
            <?php $hour = str_pad($i, 2, '0', STR_PAD_LEFT); ?>
            <?php if ($hour == $fixed_time_hour || !$fixed_time_hour) { ?>
            <?php $fixed_time_hour = $hour; ?>
            <option value="<?php echo $hour; ?>" selected="selected"><?php echo $hour; ?></option>
            <?php } else { ?>
            <option value="<?php echo $hour; ?>"><?php echo $hour; ?></option>
            <?php } ?>
            <?php } ?>
          </select>
          <select id="speedy_fixed_time_min" name="fixed_time_min" <?php if (!$fixed_time_cb) { ?> disabled="disabled"<?php } ?>>
            <?php $min_fixed_time_mins = ($fixed_time_hour == 10 ? 30 : 0); ?>
            <?php $max_fixed_time_mins = ($fixed_time_hour == 17 ? 30 : 59); ?>
            <?php for ($i = $min_fixed_time_mins; $i <= $max_fixed_time_mins; $i++) { ?>
            <?php $hour = str_pad($i, 2, '0', STR_PAD_LEFT); ?>
            <?php if ($hour == $fixed_time_min) { ?>
            <option value="<?php echo $hour; ?>" selected="selected"><?php echo $hour; ?></option>
            <?php } else { ?>
            <option value="<?php echo $hour; ?>"><?php echo $hour; ?></option>
            <?php } ?>
            <?php } ?>
          </select>
          <?php echo $text_fixed_time; ?>
          <?php if ($error_fixed_time) { ?>
          <span class="error"><?php echo $error_fixed_time; ?></span>
          <?php } ?></td>
      </tr>
    </table>
  </div>
  <div class="content" <?php if (!$cod_status) { ?> style="display: none;"<?php } ?>>
    <table style="display:none;">
      <tr>
        <td><?php echo $entry_cod; ?></td>
        <td><input checked="checked" type="radio" id="speedy_cod_yes" name="cod" value="1" <?php if ($cod) { ?> checked="checked"<?php } ?> />
          <label for="speedy_cod_yes"><?php echo $text_yes; ?></label>
          <input type="radio" id="speedy_cod_no" name="cod" value="0" <?php if (!$cod && !is_null($cod)) { ?> checked="checked"<?php } ?> />
          <label for="speedy_cod_no"><?php echo $text_no; ?></label>
          <?php if ($error_cod) { ?>
          <span class="error"><?php echo $error_cod; ?></span>
          <?php } ?></td>
      </tr>
    </table>
  </div>
  <div class="buttons">
    <div class="left" style="width:70%"><?php echo $text_calculate; ?></div>
    <div class="right"  style="width:20%"><input type="button" id="button-speedy-calculate" value="<?php echo $button_calculate; ?>" onclick="speedySubmit(false);" class="button" /></div>
  </div>
</form>
<script type="text/javascript"><!--
$('#button-shipping-method').die();
$('#button-shipping-method').live('click', function() {
	if ($('[name="shipping_method"][id^="speedy."]:checked').length) {
		speedySubmit(true);
	} else {
		speedyShipping(true);
	}
});

function speedySubmit(next) {
	$('.wait').remove();
	$('#speedy_form').prepend('<div class="wait"><img src="catalog/view/theme/default/image/loading.gif" alt="" /></div>');
	speedy_disabled = $('#speedy_form :input :disabled');
	$('#speedy_form :input').removeAttr('disabled');

	$.ajax({
		url: 'index.php?route=shipping/speedy',
		type: 'POST',
		data: $('#speedy_form').serialize(),
		dataType: 'json',
		complete: function() {
			speedy_disabled.attr('disabled', true);
		},
		success: function(data) {
			if (data) {
				if (data.redirect) {
					location = data.redirect;
				} else if (data.submit) {
					speedyShipping(next);
				} else {
					$('#speedy_container').html(data.html);
				}
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
}

function speedyShipping(next) {
	$.ajax({
		url: 'index.php?route=checkout/shipping_method/validate',
		type: 'post',
		data: $('#shipping-method input[type=\'radio\']:checked, #shipping-method textarea'),
		dataType: 'json',
		beforeSend: function() {
			$('#button-speedy-calculate').attr('disabled', true);
			$('#button-shipping-method').attr('disabled', true);
			$('#button-shipping-method').after('<span class="wait">&nbsp;<img src="catalog/view/theme/default/image/loading.gif" alt="" /></span>');
		},
		complete: function() {
			$('#button-speedy-calculate').attr('disabled', false);
			$('#button-shipping-method').attr('disabled', false);
		},
		success: function(json) {
			$('.wait').remove();
			$('.warning').remove();

			if (json['redirect']) {
				location = json['redirect'];
			}

			if (json['error']) {
				if (json['error']['warning']) {
					$('#shipping-method .checkout-content').prepend('<div class="warning" style="display: none;">' + json['error']['warning'] + '</div>');

					$('.warning').fadeIn('slow');
				}
			} else {
				$('#speedy_form').prepend('<div class="wait"><img src="catalog/view/theme/default/image/loading.gif" alt="" /></div>');

				$.ajax({
					url: 'index.php?route=checkout/shipping_method',
					dataType: 'html',
					success: function(html) {
						$('.wait').remove();
						$('#shipping-method .checkout-content').html(html);

						if (next) {
							$.ajax({
								url: 'index.php?route=checkout/payment_method',
								dataType: 'html',
								success: function(html) {
									$('#payment-method .checkout-content').html(html);

									$('#shipping-method .checkout-content').slideUp('slow');

									$('#payment-method .checkout-content').slideDown('slow');

									$('#shipping-method .checkout-heading a').remove();
									$('#payment-method .checkout-heading a').remove();

									$('#shipping-method .checkout-heading').append('<a><?php echo $text_modify; ?></a>');
								},
								error: function(xhr, ajaxOptions, thrownError) {
									//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
								}
							});
						}
					},
					error: function(xhr, ajaxOptions, thrownError) {
						//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}
		},
		error: function(xhr, ajaxOptions, thrownError) {
			//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
		}
	});
}

function speedyCheckFixedTime() {
	if ($('#speedy_fixed_time_cb:checked').length) {
		$('#speedy_fixed_time_hour').removeAttr('disabled');
		$('#speedy_fixed_time_min').removeAttr('disabled');
	} else {
		$('#speedy_fixed_time_hour').attr('disabled', 'disabled');
		$('#speedy_fixed_time_min').attr('disabled', 'disabled');
	}
}

function speedySetFixedTime() {
	if ($('#speedy_fixed_time_hour').val() == 10) {
		min_fixed_time_mins = 30;
	} else {
		min_fixed_time_mins = 0;
	}

	if ($('#speedy_fixed_time_hour').val() == 17) {
		max_fixed_time_mins = 30;
	} else {
		max_fixed_time_mins = 59;
	}

	html = '';

	for (i = min_fixed_time_mins; i <= max_fixed_time_mins; i++) {
		iStr = i.toString();

		if (iStr.length < 2) {
			fixed_time_min = '0' + i;
		} else {
			fixed_time_min = i;
		}

		html += '<option value="' + fixed_time_min + '">' + fixed_time_min + '</option>';
	}

	$('#speedy_fixed_time_min').html(html);
}

var speedy_city = '<?php echo $city; ?>';
var speedy_quarter = '<?php echo $quarter; ?>';
var speedy_street = '<?php echo $street; ?>';

$(document).ready(function() {
	$('#speedy_city').autocomplete({
		source: function(request, response) {
			var $this = $(this);
			var $element = $(this.element);
			var jqXHR = $element.data('jqXHR');
			if (jqXHR) {
				jqXHR.abort();
			}
			$element.data('jqXHR', $.ajax({
				url: 'index.php?route=shipping/speedy/getCities',
				dataType: 'json',
				data: {
					term: request.term
				},
				complete: function() {
					$this.removeData('jqXHR');
				},
				success: function(data) {
					if (data.error) {
						alert(data.error);
					} else {
						if (data.length) {
							response(data);
						}
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			}));
		},
		minLength: 1,
		select: function(event, ui) {
			if (ui.item) {
				speedy_city = ui.item.value;
				$('#speedy_postcode').val(ui.item.postcode);
				$('#speedy_city_id').val(ui.item.id);
				$('#speedy_city_nomenclature').val(ui.item.nomenclature);
				$('#speedy_quarter').val('');
				$('#speedy_quarter_id').val('');
				$('#speedy_street').val('');
				$('#speedy_street_id').val('');
				$('#speedy_street_no').val('');
				$('#speedy_block_no').val('');
				$('#speedy_entrance_no').val('');
				$('#speedy_floor_no').val('');
				$('#speedy_apartment_no').val('');
				$('#speedy_note').val('');
				$('#speedy_office_id').html('<option value="0"><?php echo $text_wait; ?></option>');
/*
				if (ui.item.nomenclature == 'NO') {
					$('#speedy_quarter, #speedy_street').autocomplete('option', 'disabled', true);
				} else {
					$('#speedy_quarter, #speedy_street').autocomplete('option', 'disabled', false);
				}
*/
				$.ajax({
					url: 'index.php?route=shipping/speedy/getOffices',
					dataType: 'json',
					data: {
						city_id: ui.item.id
					},
					success: function(data) {
						if (data.error) {
							alert(data.error);
						} else {
							html = '';

							if (data.length) {
								for (i = 0; i < data.length; i++) {
									html += '<option value="' + data[i]['id'] + '">' + data[i]['label'] + '</option>';
								}
							} else {
								html += '<option value="0"><?php echo $text_select_city; ?></option>';
							}

							$('#speedy_office_id').html(html);
						}
					},
					error: function(xhr, ajaxOptions, thrownError) {
						//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
					}
				});
			}
		},
		change: function(event, ui) {
			if(!ui.item) {
				$('#speedy_city').val('');
				$('#speedy_city_id').val('');
				$('#speedy_city_nomenclature').val('');
				$('#speedy_postcode').val('');
				$('#speedy_office_id').html('<option value="0"><?php echo $text_select_city; ?></option>');
			}

			$('#speedy_quarter').val('');
			$('#speedy_quarter_id').val('');
			$('#speedy_street').val('');
			$('#speedy_street_id').val('');
			$('#speedy_street_no').val('');
			$('#speedy_block_no').val('');
			$('#speedy_entrance_no').val('');
			$('#speedy_floor_no').val('');
			$('#speedy_apartment_no').val('');
			$('#speedy_note').val('');
		}
	});

	$('#speedy_city').blur(function() {
		var $this = $(this);
		var jqXHR = $(this).data('jqXHR');
		if (jqXHR) {
			jqXHR.abort();
		}
		$this.removeData('jqXHR');

		if ($this.val() != speedy_city) {
			$('#speedy_city').val('');
			$('#speedy_city_id').val('');
			$('#speedy_city_nomenclature').val('');
			$('#speedy_postcode').val('');
			$('#speedy_office_id').html('<option value="0"><?php echo $text_select_city; ?></option>');
			$('#speedy_quarter').val('');
			$('#speedy_quarter_id').val('');
			$('#speedy_street').val('');
			$('#speedy_street_id').val('');
			$('#speedy_street_no').val('');
			$('#speedy_block_no').val('');
			$('#speedy_entrance_no').val('');
			$('#speedy_floor_no').val('');
			$('#speedy_apartment_no').val('');
			$('#speedy_note').val('');
		}
	});

	$('#speedy_quarter').autocomplete({
		source: function(request, response) {
			var $this = $(this);
			var $element = $(this.element);
			var jqXHR = $element.data('jqXHR');
			if (jqXHR) {
				jqXHR.abort();
			}
			$element.data('jqXHR', $.ajax({
				url: 'index.php?route=shipping/speedy/getQuarters',
				dataType: 'json',
				data: {
					term: request.term,
					city_id: function() { return $('#speedy_city_id').val(); }
				},
				complete: function() {
					$this.removeData('jqXHR');
				},
				success: function(data) {
					if (data.error) {
						$('#speedy_quarter').val('');
						$('#speedy_quarter_id').val('');
						alert(data.error);
					} else {
						if ($('#speedy_city_nomenclature').val() == 'FULL') {
							if (data.length) {
								response(data);
							}
						} else {
							response(data);
						}
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			}));
		},
		minLength: 1,
		select: function(event, ui) {
			if (ui.item) {
				speedy_quarter = ui.item.value;
				$('#speedy_quarter_id').val(ui.item.id);
			}
		},
		change: function(event, ui) {
			if(!ui.item && $('#speedy_city_nomenclature').val() == 'FULL') {
				$('#speedy_quarter').val('');
				$('#speedy_quarter_id').val('');
			}
		}
	});

	$('#speedy_quarter').blur(function() {
		var $this = $(this);
		var jqXHR = $(this).data('jqXHR');
		if (jqXHR) {
			jqXHR.abort();
		}
		$this.removeData('jqXHR');

		if (($this.val() != speedy_quarter) && ($('#speedy_city_nomenclature').val() == 'FULL')) {
			$('#speedy_quarter').val('');
			$('#speedy_quarter_id').val('');
		}
	});

	$('#speedy_street').autocomplete({
		source: function(request, response) {
			var $this = $(this);
			var $element = $(this.element);
			var jqXHR = $element.data('jqXHR');
			if (jqXHR) {
				jqXHR.abort();
			}
			$element.data('jqXHR', $.ajax({
				url: 'index.php?route=shipping/speedy/getStreets',
				dataType: 'json',
				data: {
					term: request.term,
					city_id: function() { return $('#speedy_city_id').val(); }
				},
				complete: function() {
					$this.removeData('jqXHR');
				},
				success: function(data) {
					if (data.error) {
						$('#speedy_street').val('');
						$('#speedy_street_id').val('');
						alert(data.error);
					} else {
						if ($('#speedy_city_nomenclature').val() == 'FULL') {
							if (data.length) {
								response(data);
							}
						} else {
							response(data);
						}
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			}));
		},
		minLength: 1,
		select: function(event, ui) {
			if (ui.item) {
				speedy_street = ui.item.value;
				$('#speedy_street_id').val(ui.item.id);
			}
		},
		change: function(event, ui) {
			if(!ui.item && $('#speedy_city_nomenclature').val() == 'FULL') {
				$('#speedy_street').val('');
				$('#speedy_street_id').val('');
			}
		}
	});

	$('#speedy_street').blur(function() {
		var $this = $(this);
		var jqXHR = $(this).data('jqXHR');
		if (jqXHR) {
			jqXHR.abort();
		}
		$this.removeData('jqXHR');

		if (($this.val() != speedy_street) && ($('#speedy_city_nomenclature').val() == 'FULL')) {
			$('#speedy_street').val('');
			$('#speedy_street_id').val('');
		}
	});

	$('#speedy_block_no').autocomplete({
		source: function(request, response) {
			var $this = $(this);
			var $element = $(this.element);
			var jqXHR = $element.data('jqXHR');
			if (jqXHR) {
				jqXHR.abort();
			}
			$element.data('jqXHR', $.ajax({
				url: 'index.php?route=shipping/speedy/getBlocks',
				dataType: 'json',
				data: {
					term: request.term,
					city_id: function() { return $('#speedy_city_id').val(); }
				},
				complete: function() {
					$this.removeData('jqXHR');
				},
				success: function(data) {
					if (data.error) {
						$('#speedy_block_no').val('');
						alert(data.error);
					} else {
						response(data);
					}
				},
				error: function(xhr, ajaxOptions, thrownError) {
					//alert(thrownError + "\r\n" + xhr.statusText + "\r\n" + xhr.responseText);
				}
			}));
		},
		minLength: 1
	});

	$('#speedy_block_no').blur(function() {
		var $this = $(this);
		var jqXHR = $(this).data('jqXHR');
		if (jqXHR) {
			jqXHR.abort();
		}
		$this.removeData('jqXHR');
	});
});
//--></script>